using System;
using System.Timers;

namespace MyLibrary
{
    public class AlarmClock
    {
        private Timer _timer;
        public event EventHandler Tick;
        public event EventHandler Alarm;

        public AlarmClock()
        {
            _timer = new Timer(1000); // 每秒触发一次Tick事件
            _timer.Elapsed += OnTick;
        }

        public void Start()
        {
            _timer.Start();
        }

        public void Stop()
        {
            _timer.Stop();
        }

        private void OnTick(object sender, ElapsedEventArgs e)
        {
            Tick?.Invoke(this, EventArgs.Empty);
        }

        public void SetAlarm(TimeSpan alarmTime)
        {
            Timer alarmTimer = new Timer(alarmTime.TotalMilliseconds);
            alarmTimer.Elapsed += OnAlarm;
            alarmTimer.AutoReset = false;
            alarmTimer.Start();
        }

        private void OnAlarm(object sender, ElapsedEventArgs e)
        {
            Alarm?.Invoke(this, EventArgs.Empty);
        }
    }
}